from django.db import models
from django.contrib.auth.models import AbstractUser
from CS2.settings import HOST

# Create your models here.

# Модель пользователя
class User(AbstractUser):
    email = models.EmailField()
    password = models.CharField(max_length=255)

    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)

    # Создание связи многие ко многим
    files = models.ManyToManyField('File', blank=True, related_name='users', through='Access')

    def get_full_name(self):
        return f"{self.first_name} {self.last_name}"


# Модель пользователя
class File (models.Model):
    file = models.FileField(upload_to='/')
    name = models.CharField(max_length=255)
    file_id = models.CharField(max_length=10)

    def get_url(self):
        return f"{HOST}/{self.file_id}"

# Связующая модель между Пользователем и Файлом.
class Access (models.Model):
    user = models.ForeignKey('User', on_delete=models.CASCADE)
    file = models.ForeignKey('File', on_delete=models.CASCADE)
    isOwner = models.BooleanField(default=False)
